﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using SD.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;

namespace SD.Controllers
{
    [RoutePrefix("me")]
    public class MeController : Controller
    {
        private SD.EF.SdContext db = new EF.SdContext();

        //GET: Me
        [Route]
        public ActionResult Index()
        {
            return View();
        }

        // GET: Me/WatchList
        [Route("WatchList")]
        public ActionResult WatchList() {

            var watchList = db.Watchs
                .Include(x => x.Film)
                .Select(x => new FilmViewModel
                {
                    Id = x.Film.Id,
                    Altersfreigabe = x.Film.Altersfreigabe,
                    BigImg = x.Film.BigImg,
                    SmallImg = x.Film.SmallImg,
                    Darsteller = x.Film.Darsteller,
                    DeName = x.Film.DeName,
                    EnName = x.Film.EnName,
                    Inhalt = x.Film.Inhalt,
                    KeyName = x.Film.KeyName,
                    Laufzeit = x.Film.Laufzeit,
                    Regisseur = x.Film.Regisseur,
                    Verleihrechte = x.Film.Verleihrechte
                }).ToList();

            return View(watchList);
        }

        // GET: Me/Buyings
        [Route("Buyings")]
        public ActionResult BuyingsList()
        {
            var userId = User.Identity.GetUserId();

            Guid userGuid;
            Guid.TryParse(userId, out userGuid);

            var watchList = db.Buyings
                .Include(x => x.Film)
                .Where(x => x.UserId == userGuid)
                .Select(x => new FilmViewModel
                {
                    Id = x.Film.Id,
                    Altersfreigabe = x.Film.Altersfreigabe,
                    BigImg = x.Film.BigImg,
                    SmallImg = x.Film.SmallImg,
                    Darsteller = x.Film.Darsteller,
                    DeName = x.Film.DeName,
                    EnName = x.Film.EnName,
                    Inhalt = x.Film.Inhalt,
                    KeyName = x.Film.KeyName,
                    Laufzeit = x.Film.Laufzeit,
                    Regisseur = x.Film.Regisseur,
                    Verleihrechte = x.Film.Verleihrechte
                }).ToList();

            return View(watchList);
        }


        // GET: Me/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Me/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Me/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Me/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Me/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Me/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Me/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
